<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wwwttttj.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="1.从上一篇validate效验 看到MethodValidationPostProcessor  很好奇spring 是如何使@validated生效的并进行代理 &#x2F;&#x2F;spring 的生命周期1.bean 的实例化2.属性注入3.所有实现BeanNameAware接口，取得beanId和name 给spring管理4.所有实现BeanFactoryAware接口  ，给spring管理5.调用">
<meta property="og:type" content="article">
<meta property="og:title" content="springBeanpostProcess的简单介绍">
<meta property="og:url" content="https://wwwttttj.gitee.io/2020/09/24/springBeanpostProcess%E7%9A%84%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D/index.html">
<meta property="og:site_name" content="wwwblog">
<meta property="og:description" content="1.从上一篇validate效验 看到MethodValidationPostProcessor  很好奇spring 是如何使@validated生效的并进行代理 &#x2F;&#x2F;spring 的生命周期1.bean 的实例化2.属性注入3.所有实现BeanNameAware接口，取得beanId和name 给spring管理4.所有实现BeanFactoryAware接口  ，给spring管理5.调用">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-24T02:40:13.000Z">
<meta property="article:modified_time" content="2020-09-24T02:40:36.230Z">
<meta property="article:author" content="wwwttttj">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://wwwttttj.gitee.io/2020/09/24/springBeanpostProcess%E7%9A%84%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>springBeanpostProcess的简单介绍 | wwwblog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="wwwblog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">wwwblog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wwwttttj.gitee.io/2020/09/24/springBeanpostProcess%E7%9A%84%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="wwwttttj">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="wwwblog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          springBeanpostProcess的简单介绍
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-09-24 10:40:13 / 修改时间：10:40:36" itemprop="dateCreated datePublished" datetime="2020-09-24T10:40:13+08:00">2020-09-24</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>1.从上一篇validate效验 看到MethodValidationPostProcessor  很好奇spring 是如何使<code>@validated</code>生效的并进行代理</p>
<p>//spring 的生命周期<br>1.bean 的实例化<br>2.属性注入<br>3.所有实现BeanNameAware接口，取得beanId和name 给spring管理<br>4.所有实现BeanFactoryAware接口  ，给spring管理<br>5.调用 所有实现BeanPostProcessors  接口的ProcessBeforeInitialization()方法<br>6.initializingBean afterPropertiesSet()(@PostConstruct)<br>7.init Bean 初始化<br>8.BeanPostProcessors的ProcessaAfterInitialization()<br>9.destroy-method或destroy()</p>
<a id="more"></a>
<p>跟踪源码 主方法如下：<br>以下为 看到MethodValidationPostProcessor postProcessAfterInitialization()主方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Object postProcessAfterInitialization(Object bean, String beanName) &#123;</span><br><span class="line">		if (bean instanceof AopInfrastructureBean) &#123;</span><br><span class="line">			&#x2F;&#x2F; Ignore AOP infrastructure such as scoped proxies.</span><br><span class="line">			return bean;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;这边是加入proxy chain 执行链</span><br><span class="line">		if (bean instanceof Advised) &#123;</span><br><span class="line">			Advised advised &#x3D; (Advised) bean;</span><br><span class="line">			if (!advised.isFrozen() &amp;&amp; isEligible(AopUtils.getTargetClass(bean))) &#123;</span><br><span class="line">				&#x2F;&#x2F; Add our local Advisor to the existing proxy&#39;s Advisor chain...</span><br><span class="line">				if (this.beforeExistingAdvisors) &#123;</span><br><span class="line">					advised.addAdvisor(0, this.advisor);</span><br><span class="line">				&#125;</span><br><span class="line">				else &#123;</span><br><span class="line">					advised.addAdvisor(this.advisor);</span><br><span class="line">				&#125;</span><br><span class="line">				return bean;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">&#x2F;&#x2F; 这边是调用jdk Proxy.newProxyInstance 或cglib 动态生成代理</span><br><span class="line">		if (isEligible(bean, beanName)) &#123;</span><br><span class="line">			ProxyFactory proxyFactory &#x3D; prepareProxyFactory(bean, beanName);</span><br><span class="line">			if (!proxyFactory.isProxyTargetClass()) &#123;</span><br><span class="line">				evaluateProxyInterfaces(bean.getClass(), proxyFactory);</span><br><span class="line">			&#125;</span><br><span class="line">			proxyFactory.addAdvisor(this.advisor);</span><br><span class="line">			customizeProxyFactory(proxyFactory);</span><br><span class="line">			return proxyFactory.getProxy(getProxyClassLoader());</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		&#x2F;&#x2F; No async proxy needed.</span><br><span class="line">		return bean;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>从这里我们可以看到，我们可以使用自定义PostProcessor处理类，使用动态代理aop对我们的目标类进行增强。</p>
<p>//spring 自动配置<br>METE-INF 下的spring.factories  可以看到ValidationAutoConfiguration</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Bean</span><br><span class="line">	@ConditionalOnMissingBean</span><br><span class="line">	public static MethodValidationPostProcessor methodValidationPostProcessor(</span><br><span class="line">			Environment environment, @Lazy Validator validator) &#123;</span><br><span class="line">		MethodValidationPostProcessor processor &#x3D; new MethodValidationPostProcessor();</span><br><span class="line">		boolean proxyTargetClass &#x3D; environment</span><br><span class="line">				.getProperty(&quot;spring.aop.proxy-target-class&quot;, Boolean.class, true);</span><br><span class="line">		processor.setProxyTargetClass(proxyTargetClass);</span><br><span class="line">		processor.setValidator(validator);</span><br><span class="line">		return processor;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/09/24/completable-%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B/" rel="prev" title="completable_异步编程">
      <i class="fa fa-chevron-left"></i> completable_异步编程
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/09/27/%E5%88%86%E4%BA%AB%E6%A1%86%E6%9E%B6%E4%B8%AD%E5%A5%BD%E7%9A%84%E6%9E%B6%E6%9E%84%E6%80%9D%E6%83%B3/" rel="next" title="分享框架中好的架构思想">
      分享框架中好的架构思想 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">wwwttttj</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">8</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/wwwttttj" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;wwwttttj" rel="noopener" target="_blank"><i class="fab fa-gitee fa-fw"></i>gitee</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wwwttttj</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
